package jcrweb.as3
{

	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import mx.controls.Alert;
	
	public class LoginUtil{
		
		private static var LOGIN_USERNAME:String = null;
		private static var LOGIN_PASSWORD:String = null;
		private static var LOGIN_USERCODE:String = null;
		private static var LOGIN_MAIL:String = null;
		private static var LOGIN_PERMISSION_DIR_PATH:String=null;
		private static var LOGIN_LANGUAGE:String=null;
		
		[Bindable]
		[Embed(source='../WebContent/images/error.gif')]
		private var iconError:Class;
		
		public function LoginUtil(){
			LanguageUtil.loadLanguageData();
		}
		public function loginCheck():void{
			var url:String = RequestUtil.LOGINCHECK_URL;
			RequestUtil.sendRequest(url,Event.COMPLETE,loginCheckComplete);
		}
		private function loginCheckComplete(event:Event):void{
			var loader:URLLoader = URLLoader(event.target); 
			var isError:String = FlexUtil.getTag(loader.data,FlexUtil.IS_ERROR);
			
			if(isError == "true" ){
				var errorMes:String = FlexUtil.getTag(loader.data,FlexUtil.ERROR_MES_TAG);
				Alert.show("Login faild!"+errorMes,"Error",0,null,null,iconError);
			}else{
				var resutlSize:String = FlexUtil.getTag(loader.data,FlexUtil.RESULT_SIZE);
				if(resutlSize != "0"){
					var allData:String = FlexUtil.getTag(loader.data,FlexUtil.RESULT_DATA)
					var isLogin:String = FlexUtil.getTag(allData,"isLogin");
					if(isLogin != "true"){
						navigateToURL(new URLRequest(RequestUtil.LOGIN_PAGE_URL),"_self");
					}else{
						LOGIN_USERCODE = FlexUtil.getTag(allData,"userCode");
						LOGIN_USERNAME = FlexUtil.getTag(allData,"userName");
						LOGIN_PASSWORD = FlexUtil.getTag(allData,"password");
						LOGIN_MAIL = FlexUtil.getTag(allData,"mail");
						LOGIN_PERMISSION_DIR_PATH = FlexUtil.getTag(allData,"permissionDirPath");
						LOGIN_LANGUAGE = FlexUtil.getTag(allData,"language");
					}
				}
			}
		}
		public function logout():void{
			RequestUtil.sendRequest(RequestUtil.LOGOUT_URL,Event.COMPLETE,logoutComplete);
		}
		private function logoutComplete(event:Event):void {
			navigateToURL(new URLRequest(RequestUtil.LOGIN_PAGE_URL),"_self");
		}
		public function login(username:String,password:String,language:String):void{		
			var para:String = new String();
			para +="&userCode="+username;
			para +="&password="+password;
			para +="&language="+language;
			para =  encodeURI(para);
			para =  encodeURI(para);
				
			var url:String = RequestUtil.LOGIN_URL+para;
			RequestUtil.sendRequest(url,Event.COMPLETE,loginComplete);
		}		
		private function loginComplete(event:Event):void {
			var loader:URLLoader = URLLoader(event.target); 
			var isError:String = FlexUtil.getTag(loader.data,FlexUtil.IS_ERROR);
			
			if(isError == "true" ){
				var errorMes:String = FlexUtil.getTag(loader.data,FlexUtil.ERROR_MES_TAG);
				Alert.show("Login faild!"+errorMes,"Error",0,null,null,iconError);
			}else{
				var resutlSize:String = FlexUtil.getTag(loader.data,FlexUtil.RESULT_SIZE);
				if(resutlSize != "0"){
					var allData:String = FlexUtil.getTag(loader.data,FlexUtil.RESULT_DATA)
					var isLogin:String = FlexUtil.getTag(allData,"isLogin");
					if(isLogin == "true"){
						navigateToURL(new URLRequest(RequestUtil.INDEX_PAGE_URL),"_self");
					}else{
						Alert.show("Login faild! The username or password is wrong!","Error",0,null,null,iconError);
					}
				}else{
					Alert.show("Login faild! The username or password is wrong!","Error",0,null,null,iconError);
				}
			}
		}
		public static function isLogin():Boolean{
			if(LOGIN_USERNAME == null || LOGIN_USERNAME == ""){
				return false;
			}
			return true;
		}
		public static function getLoginUsername():String{
			return LOGIN_USERNAME;
		}

		public static function getLoginPassword():String{
			return LOGIN_PASSWORD;
		}
		
		public static function getLoginUsercode():String{
			return LOGIN_USERCODE;
		}
		
		public static function getLoginMail():String{
			return LOGIN_MAIL;
		}
		
		public static function getLoginPermissionDirPath():String{
			return LOGIN_PERMISSION_DIR_PATH;
		}
		
		public static function getLoginLanguage():String{
			return LOGIN_LANGUAGE;
		}
		
	}
}